package com.koron.common.web.transmit.service.impl;

import com.koron.bean.base.Response;
import com.koron.common.core.business.datadict.utils.DataDictTool;
import com.koron.common.core.util.message.MessageUtil;
import com.koron.common.web.follow.bean.followBean;
import com.koron.common.web.follow.mapper.FollowMapper;
import com.koron.common.web.transmit.bean.TransmitBean;
import com.koron.common.web.transmit.service.transmitService;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class TransmitServiceImpl implements transmitService {
    @Autowired
    MessageUtil messageUtil;

    @Override
    @TaskAnnotation("transmit")
    public Response transmit(SessionFactory factory, TransmitBean transmitBean) {
        FollowMapper mapper = factory.getMapper(FollowMapper.class);
        followBean follow = new followBean();
        //把modole的key转换成value
        Map<String, String> list = DataDictTool.getDataDictByCode("publicDictionary", "follow_DB");
        String table = null;
        for (String s : list.keySet()) {
            if (s.equals(transmitBean.getModule())) {
                table = list.get(s);
                break;
            }
        }
        if (table == null) return Response.fail("module出错！");
        //根据表名去获取当前关注页面的标题
        follow.setPage_id(transmitBean.getPage_id());
        followBean bean = mapper.getfollowlist(follow, table);
        if (bean.getTitle() == null || bean.getName() != null) transmitBean.setTitle(bean.getName());
        else if (bean.getTitle() != null || bean.getName() == null) transmitBean.setTitle(bean.getTitle());
        else return Response.success("该页面标题为空！");
        log.info(transmitBean.toString());
        String s = messageUtil.generateQywxHref(transmitBean.getTransmit_module(), transmitBean.getPage_id(),transmitBean.getPage_type(),transmitBean.getPage_id());
        log.info("企微消息发送结果：{}",s);
        messageUtil.sendMessageWithTemplate("work_transmit", s, transmitBean.getTransTo(), transmitBean.getName(), transmitBean.getTitle());
        return Response.success("发送成功!");
    }
}
